<?php
declare (strict_types=1);

namespace app\course\logic\exam\admin;

use app\common\exception\LogicException;
use app\common\helper\util\Page;
use app\common\logic\category\Category;
use app\course\logic\base\admin\Course;
use app\course\model\exam\admin\CourseExamLink as CourseExamLinkModel;
use app\exam\logic\admin\Paper;
use think\annotation\Inject;

/**
 * Class CourseExamLink
 *
 * @package app\course\logic\exam\admin
 */
class CourseExamLink extends CourseExamLinkModel
{

    /**
     * @Inject()
     *
     * @var Category
     */
    protected $categoryLogic;

    /**
     * @Inject()
     *
     * @var Paper
     */
    protected $paperLogic;

    /**
     * 获取列表
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/9/28
     *
     * @param array $map
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getList(array $map = [])
    {
        return $this->with(['paperTitle'])->where($map)->order('update_time', 'desc')->paginate($this->getListRows());
    }

    /**
     * 添加课程考试试卷
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/9/28
     *
     * @param int $courseId
     * @param     $data
     * @return $this
     */
    public function createCourseExamLink(int $courseId, $data)
    {
        $data['course_id'] = $courseId;
        // 验证数据
        $this->getValidate()->failException()->check($data);

        if (!$this->data($data, true)->save()) {
            throw new LogicException('添加失败');
        }

        // $courseTitle  = app(Course::class)->where('id', $data['course_id'])->value('title');
        // Log::write('添加【' . $courseTitle . '】关联到班级课【' . $classesTitle . '】');

        return $this;
    }

    /**
     * 编辑课程考试试卷
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/9/29
     *
     * @param int $courseId
     * @param int $paperId
     * @param     $data
     */
    public function editCourseExamLink(int $courseId, int $paperId, $data)
    {
        $data['course_id'] = $courseId;
        $data['paper_id']  = $paperId;
        $link              = $this->where('course_id', $courseId)->where('paper_id', $paperId)->findOrFail();
        // 验证数据
        $this->getValidate()->failException()->check($data);
        $link->pass_score = $data['pass_score'];

        if (!$link->save()) {
            throw new LogicException('编辑失败');
        }
    }

    /**
     * 移除课程考试试卷
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/9/29
     *
     * @param int $courseId
     * @param int $paperId
     */
    public function deleteCourseExamLink(int $courseId, int $paperId)
    {
        $link = $this->where('course_id', $courseId)->where('paper_id', $paperId)->findOrFail();
        if (!$link->delete()) {
            throw new LogicException("删除失败");
        }
    }

    /**
     * 批量移除课程考试试卷
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/9/29
     *
     * @param int $courseId
     * @param     $ids
     */
    public function deleteCourseExamLinks(int $courseId, $ids)
    {
        $links = $this->where('course_id', $courseId)->whereIn('id', (string)$ids)->selectOrFail();

        $links->each(function ($item) {
            if (!$item->delete()) {
                throw new LogicException('删除失败');
            }
        });
    }

    /**
     * 获取课程试卷
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/10/8
     *
     * @param int   $courseId
     * @param array $map
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getChooseList(int $courseId, array $map)
    {
        $courseMhm = Course::withoutGlobalScope(['mhm'])->where('id', $courseId)->value('mhm_id');
        return $this->paperLogic
            ->withoutGlobalScope(['mhm'])
            ->where($map)
            ->where('mhm_id', $courseMhm)
            ->order('update_time', 'desc')
            ->append(['paper_type_text'])
            ->paginate(Page::listRows())
            ->each(function ($item) use ($courseId) {
                $item->cate_title = $this->categoryLogic->parseTitle($item->category, 3);
                // 判断是否已经选择了
                $hasPaper      = $this
                    ->where('course_id', $courseId)
                    ->where('paper_id', $item->getAttr('id'))
                    ->value('id');
                $item->checked = !!$hasPaper;
            });
    }

    /**
     * 获取试卷配置的通过分数
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/11/22
     *
     * @param int $courseId
     * @param int $paperId
     * @return mixed
     */
    public function getPassScore(int $courseId, int $paperId)
    {
        return (int)$this
            ->where('course_id', $courseId)
            ->where('paper_id', $paperId)
            ->value('pass_score');
    }
}